// pagesProperty/pages/asset/check/add/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"),
formatTime = require("../../../../../component/common.js");
import Toast from '../../../../../component/vant/toast/toast';
import Dialog from '../../../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checkState:-1,
    assetId:0,
    fileList1:[],
    submitState: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.assetId = options.id;
    }
  },
  goSubmit(){

    if(this.data.checkState == -1){
      Toast('请选择资产状态')
      return;
    }

    if(this.data.fileList1.length == 0){
      Toast('请上传当前的资产图片，不少于一张')
      return;
    }

    this.data.submitState = true;
    this.setData({submitState : true })
    this.onHttpSubmit();
  },
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/stock/taking', {
      asset_id: that.data.assetId,
      check_state: that.data.checkState,
    }, function (res) {
      console.log("获取资产信息", res)
      if (res.data.code == 0) {
        var record_id = res.data.data;
        Toast.success(res.data.msg);
        if (record_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
            url: fetchApi.getPlatformUrl() + '/estate/asset/uploadImg',
            filePath: that.data.fileList1[index].url,
            name: 'file',
            formData: {
              record_id: record_id,
            },
            success: function (res) {
              console.log('uploadFile success res', res)
            }, fail: function (res) {
              console.log('uploadFile fail res', res)
            }
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: -1,
          })
        }, 1000)
      }else{
        Toast.fail(res.data.msg)
      }
      
      that.data.submitState = false;
      that.setData({submitState : false })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  onChange(e){
    console.log('onChange', e)
    this.data.checkState = e.detail;
  },
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
})